﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using Model.Entities;

namespace DAL
{
    public class SyncarInitializer : DropCreateDatabaseAlways<SyncarContext>
    {
        protected override void Seed(SyncarContext context)
        {
            InitUserVehicles(context);
        }

        private void InitUserVehicles(SyncarContext context)
        {
            // Insert some generic Vehicles

            var vehicles = new List<Vehicle>
            {
                new Vehicle { Category = Vehicle.EnumCategory.Car, VehicleManufacturer = new VehicleManufacturer() { Name = "Voswagen" },   Brand = "Gol" , ModelYear = 1994},
                new Vehicle { Category = Vehicle.EnumCategory.Car, VehicleManufacturer = new VehicleManufacturer() { Name =  "Renault" },   Brand = "Clio" , ModelYear = 1996 },
                new Vehicle { Category = Vehicle.EnumCategory.Car, VehicleManufacturer = new VehicleManufacturer() { Name =  "Citroen" },   Brand = "C3" , ModelYear = 2006 },
                new Vehicle { Category = Vehicle.EnumCategory.Car, VehicleManufacturer = new VehicleManufacturer() { Name =  "Ford" },   Brand = "Ka" , ModelYear = 2002 }
            };
            vehicles.ForEach(item => context.Vehicles.Add(item));
            context.SaveChanges();

            // Now we add a veicle for the user jmendiol

            var jmendiola = context.Users.Create();
            jmendiola.MembershipID = Guid.Parse("8372736c-4e1c-48be-902f-13dd1319d17d");
            jmendiola.Name = "Julian";
            jmendiola.LastName = "Mendiola";
            jmendiola.UserName = "jmendiola";

            context.SaveChanges();

            var julianVeh = new UserVehicle
            {
                ShortDesc = "Este es mi Renault Clio",
                LongDesc = "Tiene algunos pronlemas de pintura pero viaja",
                State = 7,
                VehicleIdCardExpiration = new DateTime(2013, 1, 1),
                VehicleIdentifier = "NTI472",
                VehicleIdentifierCard = "85437879thh845984",
                Year = 1996,
                User = jmendiola,
                Vehicle = context.Vehicles.Find(1),
            };

            context.UserVehicles.Add(julianVeh);
            context.SaveChanges();

            var jpublish = context.Publications.Create();
            jpublish.Description = "Quieries alguilar mi Clio?";
            jpublish.MaxDistance = new Model.Helpers.Distance() { Kilometers = 500 };
            jpublish.Title = "Renaul Clio para viajes";
            jpublish.UserVehicle = context.UserVehicles.Find(1);
            jpublish.PublicationScope = new PublicationScope() { ValidFrom = new DateTime(DateTime.Today.Year, 1, 1), ValidTo = new DateTime(DateTime.Today.Year + 1, 1, 1) };
            
            context.SaveChanges();

            jmendiola.Publications = new List<Publication>() { jpublish };
            context.SaveChanges();
            
            // Now we add a veicle for the user bgfava
            var bgarcia = context.Users.Create();
            bgarcia.Name = "Belen";
            bgarcia.LastName = "Garcia Fava";
            bgarcia.UserName = "bgarcia";
            bgarcia.MembershipID = Guid.Parse("f13250f4-bc28-42b6-979b-7829f09edc9f");
            context.SaveChanges();

            var beluVeh = new UserVehicle
            {
                ShortDesc = "Este es mi Gol Super Power",
                LongDesc = "Es una super nave",
                State = 9,
                VehicleIdCardExpiration = new DateTime(2013, 1, 1),
                VehicleIdentifier = "KUB568",
                VehicleIdentifierCard = "854378nuehh845984",
                Year = 2004,
                User = bgarcia,
                Vehicle = context.Vehicles.Find(2),
            };
            context.UserVehicles.Add(beluVeh);
            context.SaveChanges();

            var bpublish = context.Publications.Create();
            bpublish.Description = "Quieries alguilar mi Lindo Gol?";
            bpublish.MaxDistance = new Model.Helpers.Distance() { Kilometers = 400 };
            bpublish.Title = "Gol para la ciudad";
            bpublish.UserVehicle = context.UserVehicles.Find(2);
            bpublish.PublicationScope = new PublicationScope() {ValidFrom = new DateTime(DateTime.Today.Year, 1, 1), ValidTo = new DateTime(DateTime.Today.Year + 1, 1, 1)};

            context.SaveChanges();

            bgarcia.Publications = new List<Publication>() { bpublish };
            context.SaveChanges();
        }
    }
}